//
//  MainTabBarController.m
//  WXWeibo
//
//  Created by apple on 16/5/9.
//  Copyright © 2016年 zic_magic. All rights reserved.
//

#import "MainTabBarController.h"
#import "ThemeButton.h"
#import "ThemeImageView.h"

@interface MainTabBarController ()

@property (strong, nonatomic) ThemeImageView *selectImage;

@end

@implementation MainTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self _createViewControllers];
    
    [self _createCustomTabBar];
    
    
    [self.tabBar setBackgroundImage:[[UIImage alloc] init]];
    [self.tabBar setShadowImage:[[UIImage alloc] init]];
}


- (void)_createViewControllers {
    

    NSArray *names = @[@"Home", @"Message", @"Profile", @"Discover", @"More"];
    NSMutableArray *navis = [NSMutableArray array];
    for (NSString *str in names) {
        
        
        UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:str bundle:nil];
        
        UINavigationController *navi = [storyBoard instantiateInitialViewController];
        
        [navis addObject:navi];
    }
    self.viewControllers = navis;
    
}





- (void)_createCustomTabBar {
    
//    NSLog(@"%@", self.tabBar.subviews);
    for (UIView *view in self.tabBar.subviews) {
        
        if ([view isKindOfClass:NSClassFromString(@"UITabBarButton")] ) {
            
            [view removeFromSuperview];
        }

    
    }

    
    for (NSInteger i = 0 ; i < 5; i++) {
        
        ThemeButton *button = [[ThemeButton alloc] initWithFrame:CGRectMake(i * kScreenWidth / 5, 0, kScreenWidth / 5, 49)];
        
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        
        button.tag = 100 + i;
        
        
        button.normalImageName = [NSString stringWithFormat:@"home_tab_icon_%ld", i + 1];
        [self.tabBar addSubview:button];
    }
    
    ThemeImageView *tabBarImg = [[ThemeImageView alloc] initWithFrame:self.tabBar.bounds];
    tabBarImg.imgName = @"mask_navbar";
    
    [self.tabBar insertSubview:tabBarImg atIndex:0];
    
    
    
    _selectImage = [[ThemeImageView alloc] initWithFrame: CGRectMake(0, 0, kScreenWidth / 5 , 49)];
    _selectImage.imgName = @"home_bottom_tab_arrow";
    
//    [self.tabBar addSubview:_selectImage];
    [self.tabBar insertSubview:_selectImage atIndex:1];
    
    
}

- (void)buttonAction:(UIButton *)button {
    
    self.selectedIndex = button.tag - 100;
    
    
    [UIView animateWithDuration:0.25 animations:^{
       
        _selectImage.center = button.center;
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
